package com.wang.friend.rabbit;

import cn.hutool.core.lang.UUID;
import com.wang.api.domain.JudgeSubmitDTO;
import com.wang.common.security.exception.ServiceException;
import com.wang.friend.domain.user.dto.UserSubmitDTO;
import com.wang.common.core.constants.RabbitMQConstants;
import com.wang.common.core.enums.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class JudgeProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 将判题任务发送到消息队列
     *
     * @param judgeSubmitDTO 用户提交的数据
     */
    public void produceMsg(JudgeSubmitDTO judgeSubmitDTO){
        //生成唯一的 messageId 并将其设置到消息对象中
        String messageId = UUID.randomUUID().toString();
        judgeSubmitDTO.setMessageId(messageId);
        try {
            rabbitTemplate.convertAndSend(RabbitMQConstants.OJ_WORK_QUEUE, judgeSubmitDTO);
        } catch (Exception e){
            log.error("生产者发送消息异常", e);
            throw new ServiceException(ResultCode.FAILED_RABBIT_PRODUCE);
        }
    }
}
